package algorithm;

//Used in Sentinel Inc.'s Defensive Driver Prototype
//Programmer: John Casey Marr
//Programmer:
//Programmer: 
//Class CS 411 Professional Workforce Development
import gui.obu.ObuDrivingDisplayPanel;

import java.util.Calendar;

import database.OBUdata;
import database.OBUdb;

import objects.Student;


public class Algorithms extends Thread {

	private Student mystudent;
	private Calendar today;
	private long T1;
	private long T2;
	private Speed_Limit Speed;
	private Distance_Detection Distance;
	private Seat_Belt_Detection Seatbelt;
	private Stop_Sign_Detection Stopsign;
        private OBUdata runtimedb;
        private OBUdb obudatabase;
        
        @Override
		public void run() {
        	startDetection();
        }
    
    private ObuDrivingDisplayPanel window_;
	private boolean threadDone = false;
	
	public Algorithms() {

	        mystudent = new Student();
	        today = Calendar.getInstance();
	        T1 = 0;
	        T2 = 0;
	        Speed = new Speed_Limit();
        	Distance=new Distance_Detection();
	        Seatbelt=new Seat_Belt_Detection();
        	Stopsign=new Stop_Sign_Detection();
	}

	
	public void setWindow(ObuDrivingDisplayPanel window) {
		this.window_=window;
	}
	
	public void done() {
		threadDone  = true;
	}

	private void startDetection() {
		mystudent.setID(4);

		Integer size=0;

		today.setTimeInMillis(System.currentTimeMillis());
		T1=today.getTimeInMillis();
		
		try {
			runtimedb = new OBUdata();
		} catch (Exception myexc2) {
			System.out.println("Coult not open connection to the OBUdata database");
		}
		try {
			obudatabase = new OBUdb();
		} catch (Exception myexc) {
			System.out.println("Coult not open connection to the OBUdb database");
		}
		try {
			size=runtimedb.getSize();
		} catch (java.sql.SQLException myexc) {
			System.out.println("Could not get the size of the runtime database");
		}
		System.out.println("The size is: " + size);

		//GUI Stuff
		Stopsign.setWindow(window_);
		Seatbelt.setWindow(window_);
		Speed.setWindow(window_);
		Distance.setWindow(window_);
		
		for(Integer i=0;i<size && !threadDone;i++) {
		
			try {
				Thread.sleep(975);
			} catch (java.lang.InterruptedException exc) {
				System.out.println("Thread interrupted");
			}
			
			Distance.compute(runtimedb,obudatabase,i+1,mystudent.getID());
			Speed.compute(runtimedb,obudatabase,i+1,mystudent.getID());
			Stopsign.compute(runtimedb,obudatabase,i+1,mystudent.getID());			
			Seatbelt.compute(runtimedb,obudatabase,i+1,mystudent.getID(),i);
		}
		
		runtimedb.close();
				
		today.setTimeInMillis(System.currentTimeMillis());
		T2=today.getTimeInMillis();

		long T_Final;
		T_Final=T2-T1;
		
		double duration = T_Final;

		duration=duration*0.001; //Converts to seconds
		duration=duration*0.0167; //Converts to Minutes
		
		// This updates the OBUdb instance
		today.setTimeInMillis(T_Final);
		try {
			obudatabase.getStudent(mystudent);
		} catch (Exception myexc2) {
			System.out.println("Could not get driver info");			
		}
		mystudent.setHours(duration);
		try {
			obudatabase.updateStudentHours(mystudent);
		} catch (Exception myexc2) {
			System.out.println("Could not set hours in DB");	
		}
		
		obudatabase.close();
	}
}

